package socket.udp.multServer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/9/30 10:29
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/9/30 10:29
 */
public class ServerThread extends Thread {
    private DatagramPacket packet;
    private DatagramSocket socket;

    public ServerThread(DatagramSocket socket,DatagramPacket packet) {
        this.socket = socket;
        this.packet = packet;
    }

    @Override
    public void run() {
        try {
            byte[] bt = new byte[packet.getLength()];
            System.arraycopy(packet.getData(), 0, bt, 0, packet.getLength());
            System.out.println(packet.getAddress().getHostAddress() + ": " + packet.getPort() + ": "
                    + Arrays.toString(bt));
            Thread.sleep(5 * 1000);
            //设置回复的数据，原数据返回，以便客户端知道是哪个客户端发的数据
            packet.setData(bt);
            socket.send(packet);
            //MultUDPServer.response(packet);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
